package com.cloudptt.api.product.func.call.group.fsm.floor;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallTimer;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.func.call.group.fsm.call.GroupCallAgent;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.AddrInfo;

import android.content.Context;


public class FloorPendingRequestState implements ISubState,IRollback
{
	private FloorAgent agent = null;
	private RollbackTimer rollbackTimer = null;
	private int c101 = 3;
	private int t101 = 1;

	public FloorPendingRequestState(Context ctx,FloorAgent agent)
	{
		CallTimer callTimer = agent.getCallTimer();
		this.c101 = callTimer.getC101();
		this.t101 = callTimer.getT101();

		rollbackTimer = new RollbackTimer(ctx,t101,this);

		this.agent = agent;

		Log.d(FloorAgent.Log_Key, "currentState is FloorPendingRequestState");
	}

	public int doUserAction(int btnAction)
	{
		if(CallConst.BtnAction_Release == btnAction)
		{
//			SystemClock.sleep(1000);
			int result = agent.floorRelease();
			//			if(CauseCont.OperateCheck_Suc == result)
			{
				agent.goToState(CallConst.SpeakState_PendingRelease,null);
			}
			return result;
		}
		return CauseCont.OperateErr_Invalid;
	}

	public void receiveRtcpMsg(RtcpMsg msg)
	{
		Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg msg = " + msg);
		if(msg == null)
		{
			return;
		}

		String title = msg.getTitle();
		if(title.equals(RtcpMsg.Title_FLOOR_DENY))
		{
			int cause = msg.getCause();
			Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg cause = " + cause);
			agent.notifyDenyCause(cause);
			agent.goToState(CallConst.FloorState_NoPermission,null);
		}
		else if(title.equals(RtcpMsg.Title_FLOOR_QUEUEINFO))
		{
			//			gotoQueuedStatus();
		}
		else if(title.equals(RtcpMsg.Title_FLOOR_TAKEN))
		{
			agent.goToState(CallConst.FloorState_NoPermission,msg);
		}
		else if(title.equals(RtcpMsg.Title_FLOOR_GRANTED))
		{
			//			gotoPermissionStatus();
			agent.goToState(CallConst.FloorState_Permission,msg);
		}
		else if(title.equals(RtcpMsg.Title_FLOOR_REVOKE))
		{
			int cause = msg.getCause();
			Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg cause = " + cause);
			//			 agent.notifyRevokeCause(cause);

			//			gotoPendingReleaseStatus();
			//			agent.getFloorMediaOperate().setRtpEnable(LTEMedia.RTP_SENDRECV_DISABLE);
			//			int result =  agent.releaseFloor();
			//			Log.d(CallAgent.Log_Key, "" + CallConst.speakStatusTag(getStatusId()) + " receiveRtcpMsg result = " + result);
			//			if(result == CallConst.Operate_Suc)
			{
				//				gotoPendingReleaseStatus();
				agent.goToState(CallConst.SpeakState_PendingRelease,null);
			}
		}
	}

	public void updateTakenUser(String callType,AddrInfo gInfo,AddrInfo user)
	{}
	public int getState()
	{
		return CallConst.FloorState_PendingRequset;
	}
	public void destory()
	{
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		agent = null;
	}

	@Override
	public void rollback() {
		// TODO Auto-generated method stub
		Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " onTimeout");
		if(c101 > 0)
		{
			if(agent != null)
			{
				int result = agent.floorRequest();
				if(result == CallConst.Operate_Suc)
				{}
			}
			if(rollbackTimer != null)
			{
				rollbackTimer.restartTimer(t101);
			}
			c101 --;
		}
		else if(agent != null)
		{
			agent.goToState(CallConst.FloorState_NoPermission,null);
		}
	}
}
